home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-09-21 | 814 b | 40 lines | [TEXT/ttxt] |
- //-------------------------------------------------------------------//
-
- // Syntax: diff ( X )
- // diff ( X , k )
-
- // Description:
-
- // The function diff computes the difference between adjacent
- // elements in a vector. The function diff merely does:
-
- // x = x[2:n] - x[1:n-1];
-
- // If X is a matrix, then the operation is performed on each column
- // of X.
-
- // The default value for `k' is 1. If k is specified as other than 1,
- // then the operation is performed k times.
-
- //-------------------------------------------------------------------//
-
- diff = function ( X, k )
- {
- local (i, m, n, x);
-
- if (!exist (k)) { k = 1; }
-
- x = X;
- for (i in 1:k)
- {
- m = size(x)[1]; n = size(x)[2];
- if (m == 1)
- {
- x = x[2:n] - x[1:n-1];
- else
- x = x[2:m;] - x[1:m-1;];
- }
- }
- return x;
- };
-